<template>
    <div>
        <el-dialog
        :title="isEdit?'修改':title"
        :visible.sync="dialogVisible"
        width="50%">
        <el-form ref="form" :model="form" label-width="100px">
            <el-form-item label="轮播图片">
                <el-upload
                    class="upload-demo"
                    accept=".jpeg,.png,.jpg,.bmp,.gif,.webp"
                    :action="uploadUrl"
                    :on-remove="handleRemove"
                    :before-remove="beforeRemove"
                    :headers="headers"
                    :on-exceed="handleExceed"
                    :file-list="fileList"
                    :on-success="handleSuccess"
                    :on-error="handleError"
                    list-type="picture-card"
                    :limit="1"
                    >
                    <el-button size="small" type="primary">点击上传</el-button>
                    <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
                </el-upload>
            </el-form-item>
            <el-form-item label="备注">
                <el-input v-model="form.remarks" clearable></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="submitHandle">确 定</el-button>
        </span>
        </el-dialog>
    </div>
</template>

<script>
    import swiper from '@/api/swiper/index'
    import { getToken } from '@/utils/auth'
    export default {
        name: 'NotiDialog',
        components: { 
        },
        props: {
            title: {
                type: String,
                default:'新增'
            },
            
        },
        computed: {
            uploadUrl(){
                return process.env.VUE_APP_BASE_API+'/public/file/upload'
            },
            headers() {
                return { Authorization: getToken() };
            },
        },
        data () {
            return {
                dialogVisible:false,
                isEdit:false,//是否编辑
                fileList: [{
                    name: '',
                    url: '',
                }],
                form: {
                    id: '',
                    url: '',
                    remarks:''
                },
            }
        },
        computed: {
            uploadUrl(){
                return process.env.VUE_APP_BASE_API+'/public/file/upload'
            },
            headers() {
                return { Authorization: getToken() };
            },
        },
        
        methods: {
            submitHandle(){
                this.form.url = this.fileList[0].url
                if (this.isEdit) {//修改
                    swiper.editHandle(this.form).then((res) => {
                        this.dialogVisible = false
                        this.$notify({ message: '操作成功', type: 'success'});
                        this.$emit("submitSuccess")
                    }).catch(() => {})
                }else{
                    swiper.addHandle(this.form).then((res) => {
                        this.dialogVisible = false
                        this.$notify({ message: '操作成功', type: 'success'});
                        this.$emit("submitSuccess")
                    }).catch(() => {})
                }
            },
            beforeRemove(file, fileList) {
                return this.$confirm(`确定移除 ${ file.name }？`);
            },
            handleSuccess(response, file, fileList) {
                console.log(file)
                this.fileList = [{
                    name: file.name,
                    url: file.response.data.url,
                }]
            },
            handleError(){
                this.$notify({ message: '上传失败', type: 'error'});
            },
            handleExceed(files, fileList) {
                this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
            },
            handleRemove(file, fileList) {
                console.log(file, fileList);
                // for (let index = 0; index < fileList.length; index++) {
                //     const element = fileList[index];
                //     if (element.id == file.id) {
                //         fileList.splice(index, 1);
                //         break;
                //     }
                // }
                // this.form.tbPlatformGoodsFileList = fileList;
            },
        }
    }
</script>

<style lang="scss">

</style>